import Vue from "vue";
import { Store, install } from "vuex";
if (!window.Vuex) {
  install(Vue);
}

export default new Store({
  state: {
    touchStatus: false,
    type: "all",
    loading: false,
    goTop: false,
    numList: {},
  },
  getters: {},
  mutations: {
    //设置商城页面是否可以左右滑动
    setTouchStatus(state, bol) {
      state.touchStatus = bol;
    },
    //设置商品排列方式
    setType(state, type) {
      state.type = type;
    },
    //设置全局loading显示
    setLoading(state, blo) {
      state.loading = blo;
    },
    //设置点击商城是否回到顶部
    setGoTop(state, bol) {
      state.goTop = bol;
    },
    //将选中商品储存
    localNumList(state, { id, num }) {
      if (state.numList[id]) {
        if ((num === -1 && state.numList[id] === 1) || num === -Infinity) {
          Vue.delete(state.numList, id);
        } else {
          Vue.set(state.numList, id, state.numList[id] + num);
        }
      } else {
        Vue.set(state.numList, id, 1);
      }
      localStorage.setItem("numList", JSON.stringify(state.numList));
    },
    //设置选中商品数据
    setNumList(state, val) {
      state.numList = val;
    },
  },
  actions: {},
  modules: {},
});
